package com.zgjl.api.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Base64;

/**
 * @Author: ShenDong
 * @Date: 2024/11/10 11:31
 * @Description:
 **/
public class JWTUtil {

    private static String jwtSecret;

    // 设置静态密钥
    public static void setJwtSecret(String secret) {
        JWTUtil.jwtSecret = secret;
    }

    // 生成JWT
    public static String createJWT(String encryptedData) {
        return Jwts.builder()
                .claim("data", encryptedData)
                .signWith(SignatureAlgorithm.HS256, Base64.getDecoder().decode(jwtSecret))
                .compact();
    }

    // JWT解析和验证
    public static String parseJWT(String jwtToken) {
        Claims claims = Jwts.parser()
                .setSigningKey(Base64.getDecoder().decode(jwtSecret))
                .parseClaimsJws(jwtToken)
                .getBody();
        return claims.get("data", String.class);
    }

}
